app.controller('sysCtrl', function ($scope, sysConfigurationService) {

    var msgBox = null;
    var titleFlag = null;

    //页面初始化加载
    function getSysConfigs() {
        sysConfigurationService.getSysConfigs({
            action: 'getSysConfigs'
        }, function (data) {
            //展示数据
            $scope.editFlag = true;
            if (data.state == 'SUCCESS') {
                $scope.qmConnNumber = data.data.Result[0].qmConnNumber;
                $scope.queueDepthThreshold = data.data.Result[0].queueDepthThreshold;
                $scope.userMonitorUrl = data.data.Result[0].userMonitorUrl;
                $scope.sysMonitorUrl = data.data.Result[0].sysMonitorUrl;
                $scope.gwNetworkMonitorUrl = data.data.Result[0].gwNetworkMonitorUrl;
                $scope.MQSysMonitorUrl = data.data.Result[0].MQSysMonitorUrl;
                $scope.adminDefaultUrl = data.data.Result[0].adminDefaultUrl;
                $scope.monitorUserDefaultUrl = data.data.Result[0].monitorUserDefaultUrl;
                $scope.gwServiceUrl = data.data.Result[0].gwServiceUrl;
                $scope.adminUserName = data.data.Result[0].adminUserName;
                $scope.adminUserPwd = null;
                $scope.monitorUserName = data.data.Result[0].monitorUserName;
                $scope.monitorUserPwd = null;
                $scope.GWMasterName = data.data.Result[0].GWMasterName;
                $scope.GWMasterIP = data.data.Result[0].GWMasterIP;
                $scope.GWSlaveName = data.data.Result[0].GWSlaveName;
                $scope.GWSlaveIP = data.data.Result[0].GWSlaveIP;
            } else {
                if (msgBox == null) {
                    msgBox = message_alert('提示',data.reason, function () {
                        msgBox = null;
                    });
                }
            }
            $("#btnSysSave").attr('disabled', 'disabled');
            $("#btnAdminUpdate").attr('disabled', 'disabled');
            $("#btnMonitorUpdate").attr('disabled', 'disabled');
        });
    };

    getSysConfigs();

    /**
     * 解锁事件
     */
    $scope.unlockPage = function () {
        $scope.editFlag = false;
        $("#btnSysSave").removeAttr('disabled', 'disabled');
        $("#btnAdminUpdate").removeAttr('disabled', 'disabled');
        $("#btnMonitorUpdate").removeAttr('disabled', 'disabled');
    }

    /**
     * 上锁事件
     */
    $scope.lockPage = function () {
        $scope.editFlag = true;
        $("#btnSysSave").attr('disabled', 'disabled');
        $("#btnAdminUpdate").attr('disabled', 'disabled');
        $("#btnMonitorUpdate").attr('disabled', 'disabled');
    }

    /**
     * 保存按钮事件
     */
    $scope.updateSys = function (invalid) {
        if (!invalid) {
            var sysArray = [];
            var sys = null;

            var qmConnNumber = $('#qmConnNumber').val();
            var queueDepthThreshold = $('#queueDepthThreshold').val();
            var userMonitorUrl = $('#userMonitorUrl').val();
            var sysMonitorUrl = $('#sysMonitorUrl').val();
            var gwNetworkMonitorUrl = $('#gwNetworkMonitorUrl').val();
            var MQSysMonitorUrl = $('#MQSysMonitorUrl').val();
            var adminDefaultUrl = $('#adminDefaultUrl').val();
            var monitorUserDefaultUrl = $('#monitorUserDefaultUrl').val();
            var gwServiceUrl = $('#gwServiceUrl').val();

            var adminUserName = $('#adminUserName').val();
            var adminUserPwd = $('#adminUserPwd').val();
            var monitorUserName = $('#monitorUserName').val();
            var monitorUserPwd = $('#monitorUserPwd').val();

            var GWMasterName = $('#GWMasterName').val();
            var GWMasterIP = $('#GWMasterIP').val();
            var GWSlaveName = $('#GWSlaveName').val();
            var GWSlaveIP = $('#GWSlaveIP').val();

            if (adminUserPwd == '' && monitorUserPwd != '') {// 只修改值班员密码
                sys = {
                    qmConnNumber: qmConnNumber,
                    queueDepthThreshold: queueDepthThreshold,
                    userMonitorUrl: userMonitorUrl,
                    sysMonitorUrl: sysMonitorUrl,
                    gwNetworkMonitorUrl : gwNetworkMonitorUrl,
                    MQSysMonitorUrl : MQSysMonitorUrl,
                    adminDefaultUrl: adminDefaultUrl,
                    monitorUserDefaultUrl: monitorUserDefaultUrl,
                    gwServiceUrl: gwServiceUrl,
                    adminUserName : adminUserName,
                    monitorUserName : monitorUserName,
                    monitorUserPwd : monitorUserPwd,

                    GWMasterName : GWMasterName,
                    GWMasterIP : GWMasterIP,
                    GWSlaveName : GWSlaveName,
                    GWSlaveIP : GWSlaveIP
                };
            } else if (adminUserPwd != '' && monitorUserPwd == '') {// 只修改管理员密码
                sys = {
                    qmConnNumber: qmConnNumber,
                    queueDepthThreshold: queueDepthThreshold,
                    userMonitorUrl: userMonitorUrl,
                    sysMonitorUrl: sysMonitorUrl,
                    gwNetworkMonitorUrl : gwNetworkMonitorUrl,
                    MQSysMonitorUrl : MQSysMonitorUrl,
                    adminDefaultUrl: adminDefaultUrl,
                    monitorUserDefaultUrl: monitorUserDefaultUrl,
                    gwServiceUrl: gwServiceUrl,
                    adminUserName : adminUserName,
                    adminUserPwd : adminUserPwd,
                    monitorUserName : monitorUserName,

                    GWMasterName : GWMasterName,
                    GWMasterIP : GWMasterIP,
                    GWSlaveName : GWSlaveName,
                    GWSlaveIP : GWSlaveIP
                };
            } else if (adminUserPwd != '' && monitorUserPwd != '') {// 管理员和值班员密码都修改
                sys = {
                    qmConnNumber: qmConnNumber,
                    queueDepthThreshold: queueDepthThreshold,
                    userMonitorUrl: userMonitorUrl,
                    sysMonitorUrl: sysMonitorUrl,
                    gwNetworkMonitorUrl : gwNetworkMonitorUrl,
                    MQSysMonitorUrl : MQSysMonitorUrl,
                    adminDefaultUrl: adminDefaultUrl,
                    monitorUserDefaultUrl: monitorUserDefaultUrl,
                    gwServiceUrl: gwServiceUrl,
                    adminUserName : adminUserName,
                    adminUserPwd : adminUserPwd,
                    monitorUserName : monitorUserName,
                    monitorUserPwd : monitorUserPwd,

                    GWMasterName : GWMasterName,
                    GWMasterIP : GWMasterIP,
                    GWSlaveName : GWSlaveName,
                    GWSlaveIP : GWSlaveIP
                };
            } else { // 管理员和值班员密码都未修改
                sys = {
                    qmConnNumber: qmConnNumber,
                    queueDepthThreshold: queueDepthThreshold,
                    userMonitorUrl: userMonitorUrl,
                    sysMonitorUrl: sysMonitorUrl,
                    gwNetworkMonitorUrl : gwNetworkMonitorUrl,
                    MQSysMonitorUrl : MQSysMonitorUrl,
                    adminDefaultUrl: adminDefaultUrl,
                    monitorUserDefaultUrl: monitorUserDefaultUrl,
                    gwServiceUrl: gwServiceUrl,
                    adminUserName : adminUserName,
                    monitorUserName : monitorUserName,

                    GWMasterName : GWMasterName,
                    GWMasterIP : GWMasterIP,
                    GWSlaveName : GWSlaveName,
                    GWSlaveIP : GWSlaveIP
                };
            }
            sysArray.push(sys);
            sysConfigurationService.updateSysConfig({
                action: 'updateSysConfig',
                sysArray: sysArray
            }, {}, function (data) {
                if (data.state == 'SUCCESS') {
                    message_alert('提示',"操作成功", function () {
                        getSysConfigs();
                    });
                } else {
                    message_alert('提示',"操作失败(" + data.reason + ")",null);
                }
            }, $scope.requestError);
        }else{
            message_alert('提示','页面信息有误，请重新填写',null);
            return
        }
    }

    /**
     * 修改密码
     * @param Object
     */
    $scope.updatePwd = function (Object) {
        $('#dialogUpdatePwd').on('show.bs.modal',function(){
            if(Object == 0) {
                $scope.title = '管理员用户密码修改';
                titleFlag = 0;
            } else {
                $scope.title = '值班员用户密码修改';
                titleFlag = 1;
            }
            $scope.pwd = '';
            $scope.pwdAgain = '';
        });
        $("#btnSysSave").removeAttr('disabled', 'disabled');
        $('#dialogUpdatePwd').modal('show');
    }


    /**
     * 取消按钮事件
     */
    $scope.cancel = function () {
        $('#dialogUpdatePwd').modal('hide');
    };

    /**
     * 文本框校验 获取焦点为false 丢失焦点为true
     */
    $scope.change = function (flag) {
        $scope.changed = flag;
    }

    /**
     * 确认按钮事件
     */
    $scope.savePwd = function(){
        if ($scope.pwd == $scope.pwdAgain) {
            if(titleFlag == 0){
                $scope.adminUserPwd = $scope.pwd;
            } else {
                $scope.monitorUserPwd = $scope.pwd;
            }
            message_alert('提示','修改成功后，请记得在初始页面点保存，否则不会生效！',null);
            $scope.cancel();
        } else {
            message_alert('提示',"两次输入密码不一致,请重新输入!",null);
        }
    }

});